package io.github.mobodev.textviewpadding;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by shaobin on 3/20/16.
 */
public class MainActivity extends AppCompatActivity {

    private static final String TAG = "TextViewPadding";
    @Bind(R.id.text1)
    TextView mTvSample1;
    @Bind(R.id.text2)
    TextView mTvSample2;

    @Bind(R.id.tv_result1)
    TextView mTvResult1;
    @Bind(R.id.tv_result2)
    TextView mTvResult2;

    @Bind(R.id.sp1)
    Spinner mSp1;
    @Bind(R.id.sp2)
    Spinner mSp2;

    private Runnable mUpdateTextViewHeightRunnable = new Runnable() {
        @Override
        public void run() {
            updateHeight();
        }


    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        List<Integer> fontSizeList = new ArrayList<>();
        for (int i = 12; i <= 56; i++) {
            fontSizeList.add(i);
        }

        final ArrayAdapter<Integer> adapter1 = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, fontSizeList);
        mSp1.setAdapter(adapter1);
        mSp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                Integer item = adapter1.getItem(i);
                mTvSample1.setTextSize(TypedValue.COMPLEX_UNIT_SP, item);
                Log.d(TAG, "adapter1 select: " + item);

                mSp1.post(mUpdateTextViewHeightRunnable);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        final ArrayAdapter<Integer> adapter2 = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, fontSizeList);
        mSp2.setAdapter(adapter2);
        mSp2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                Integer item = adapter2.getItem(i);
                mTvSample2.setTextSize(TypedValue.COMPLEX_UNIT_SP, item);
                Log.d(TAG, "adapter2 select: " + item);

                mSp2.post(mUpdateTextViewHeightRunnable);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            updateHeight();
        }
    }

    private void updateHeight() {
        mTvResult1.setText(String.format(Locale.CHINA, "TextView1\nfont: %.0f px\nHeight: %d px", mTvSample1.getTextSize(), mTvSample1.getHeight()));
        mTvResult2.setText(String.format(Locale.CHINA, "TextView2\nfont: %.0f px\nHeight: %d px", mTvSample2.getTextSize(), mTvSample2.getHeight()));
    }
}
